<?php
include('../include/config.php');

$fullName = $_POST['fullName'];
$accountName = $_POST['username'];
$password = $_POST['password'];
$passwordRetype = $_POST['passwordRetype'];
$email = $_POST['email'];
$phoneNo = $_POST['phone'];
$regex_mail = '/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$regex_phone = '/^[0-9]+$/';

if($accountName == '') {
    header('Location: user-form.php?error=2');
}
else if($password == '') {
    header('Location: user-form.php?error=3');
}
else if($email == '') {
    header('Location: user-form.php?error=4');
}
else if($fullName == '') {
    header('Location: user-form.php?error=5');
}
else if($phoneNo == '') {
    header('Location: user-form.php?error=6');
}
else if($password !== $passwordRetype) {
    header('Location: user-form.php?error=7');
}
else if(!check_username($accountName)) {
    header('Location: user-form.php?error=8');
}
else if(!check_email($email)) {
    header('Location: user-form.php?error=9');
}
else if(!preg_match($regex_mail, $email)) {
    header('Location: user-form.php?error=10');
}
else if(!preg_match($regex_phone, $phoneNo)) {
    header('Location: user-form.php?error=11');
}
else {
    $userInfo = array();
    $userInfo['username'] = $accountName;
    $userInfo['password'] = $password;
    $userInfo['fullname'] = $fullName;
    $userInfo['email'] = $email;
    $userInfo['tel'] = $phoneNo;

    add_user($userInfo);
    echo "Success";
}
?>